/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: MailController
 * Author:   zhouheng
 * Date:     2018/7/29 12:59
 * Description: 邮件发送控制类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.modular.mail.controller;

import com.hengby.hengcrud.core.exception.HengCrudException;
import com.hengby.hengcrud.core.exception.HengCrudExceptionEnum;
import com.hengby.hengcrud.modular.base.controller.BaseController;
import com.hengby.hengcrud.modular.base.tips.SuccessTips;
import com.hengby.hengcrud.modular.mail.model.Email;
import com.hengby.hengcrud.modular.mail.service.IMailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈邮件发送控制类〉
 *
 * @author zhouheng
 * @create 2018/7/29
 * @since 1.0.0
 */
@Controller
@RequestMapping(value = "/mail")
public class MailController extends BaseController{
    @Autowired
    private IMailService mailService;


    @ApiOperation(value = "发送email",httpMethod = "POST")
    @RequestMapping("/send")
    @ResponseBody
    public Object send(@RequestBody Email email, BindingResult result){
        if (result.hasErrors()){
            throw new HengCrudException(HengCrudExceptionEnum.REQUEST_NULL);
        }

        try {
            mailService.sendFreemarker(email);
        } catch (Exception e) {
            e.printStackTrace();
            throw new HengCrudException(HengCrudExceptionEnum.SEND_EMAIL_ERROR);
        }

        return new SuccessTips();
    }

    @RequestMapping("/list")
    public Object list(){
        List<Map<String,Object>> list = mailService.findAll();

        return list;
    }


}